package com.newly.center.business.service.impl;

import cn.hutool.json.JSONObject;
import com.newly.center.business.service.MQTTBlackListService;
import com.newly.common.base.entity.R;
import com.newly.driver.mqtt.utils.MqttUtils;
import com.newly.driver.mqtt.utils.ReturnResult;
import com.newly.transfer.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("mqttBlacklistService")
public class MQTTBlackListServiceImpl implements MQTTBlackListService {
    @Autowired
    RedisUtils redisUtils;

    @Override
    public R getBlackList(String productKey, int start, int limit) {
        String token = redisUtils.get("mqtt"+productKey,String.class);
        String requestTopic = "/sys/device/"+ productKey+"/command/request/request-id=0x60000001";
        String responseTopic = "/sys/device/"+ productKey+"/command/response/request-id=0x60000001";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id",productKey);
        result.put("token",token);
        JSONObject params = new JSONObject();
        JSONObject page = new JSONObject();
        page.put("start",start);
        page.put("limit",limit);
        params.put("page",page);
        result.put("params",params);

        MqttUtils.publishMessage(requestTopic,result,1);
        return ReturnResult.getResult(responseTopic,redisUtils);
    }

    @Override
    public R modifyBlackList(String productKey, int index, String plateNo) {
        String token = redisUtils.get("mqtt"+productKey,String.class);
        String requestTopic = "/sys/device/"+ productKey+"/command/request/request-id=0x60000002";
        String responseTopic = "/sys/device/"+ productKey+"/command/response/request-id=0x60000002";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id",productKey);
        result.put("token",token);
        JSONObject params = new JSONObject();
        params.put("index",index);
        params.put("plate-no",plateNo);
        result.put("params",params);

        MqttUtils.publishMessage(requestTopic,result,1);
        return ReturnResult.getResult(responseTopic,redisUtils);
    }

    @Override
    public R deleteBlackList(String productKey,int index) {
        String token = redisUtils.get("mqtt"+productKey,String.class);
        String requestTopic = "/sys/device/"+ productKey+"/command/request/request-id=0x60000003";
        String responseTopic = "/sys/device/"+ productKey+"/command/response/request-id=0x60000003";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id", productKey);
        result.put("token",token);
        JSONObject params = new JSONObject();
        params.put("index",index);
        result.put("params",params);

        MqttUtils.publishMessage(requestTopic,result,1);
        return ReturnResult.getResult(responseTopic,redisUtils);
    }

    @Override
    public R addBlackList(String productKey,String[] plate) {
        String token = redisUtils.get("mqtt"+productKey,String.class);
        String requestTopic = "/sys/device/"+ productKey+"/command/request/request-id=0x60000004";
        String responseTopic = "/sys/device/"+ productKey+"/command/response/request-id=0x60000004";

        JSONObject result = new JSONObject();
        result.put("version","1.0");
        result.put("id",productKey);
        result.put("token",token);
        JSONObject params = new JSONObject();

        params.put("plates",plate);
        result.put("params",params);

        MqttUtils.publishMessage(requestTopic,result,1);
        return ReturnResult.getResult(responseTopic,redisUtils);
    }
}
